#include "mousedraggraphics.h"
#include "ui_mousedraggraphics.h"

MouseDragGraphics::MouseDragGraphics(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MouseDragGraphics)
{
    ui->setupUi(this);


}

MouseDragGraphics::~MouseDragGraphics()
{
    delete m_painter;
    delete ui;
}


void MouseDragGraphics::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_mouseIsPressed = true;
    }
    if (m_mouseIsPressed && m_isDrawing ) {
        m_startPoint = event->pos();
    }
}

void MouseDragGraphics::mouseMoveEvent(QMouseEvent *event)
{
    if (m_mouseIsPressed && m_isDrawing) {
        m_endPoint = event->pos();
        update();
    }
}

void MouseDragGraphics::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->buttons() == Qt::NoButton) {
        m_mouseIsPressed = false;
    }
    if (m_isDrawing) {
        m_endPoint = event->pos();
        update();
    }
}

void MouseDragGraphics::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QRect rect(m_startPoint, m_endPoint);
    QPainter painter(this);
    QPen pen(m_penColor, 4, Qt::PenStyle::SolidLine);
    pen.setStyle(Qt::PenStyle::SolidLine);
    QBrush brush;
    brush.setStyle(Qt::BrushStyle::SolidPattern);
    brush.setColor(m_brushColor);
    painter.setPen(pen);
    painter.setBrush(brush);
    painter.drawRect(rect);
}
